package com.example.controller;

import com.example.entity.RestBean;
import com.example.entity.dto.Account;
import com.example.entity.dto.AccountDetails;
import com.example.entity.dto.AccountPrivacy;
import com.example.entity.vo.request.ChangePasswordVo;
import com.example.entity.vo.request.DetailsSaveVo;
import com.example.entity.vo.request.ModifyEmailVo;
import com.example.entity.vo.request.PrivacySaveVo;
import com.example.entity.vo.response.AccountDetailsVo;
import com.example.entity.vo.response.AccountPrivacyVo;
import com.example.entity.vo.response.AccountVo;
import com.example.service.AccountDetailsService;
import com.example.service.AccountPrivacyService;
import com.example.service.AccountService;
import com.example.utils.Const;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;
import java.util.function.Supplier;

/**
 * @Author: shenjianZ
 * @Date: 2024/4/19 下午9:00
 * @Description:
 */

@RequestMapping("/api/user")
@RestController
public class AccountController {
    @Resource
    private AccountService accountService;
    @Resource
    private AccountDetailsService accountDetailsService;
    @Resource
    private AccountPrivacyService accountPrivacyService;

    @GetMapping("info")
    public RestBean<AccountVo> info(@RequestAttribute(Const.ATTR_USER_ID) int userId) {
        Account account = accountService.findAccountById(userId);
        return RestBean.success(account.asViewObject(AccountVo.class));
    }


    @GetMapping("details")
    public RestBean<AccountDetailsVo> details(@RequestAttribute(Const.ATTR_USER_ID) int userId) {
        AccountDetails details = Optional
                .ofNullable(accountDetailsService.findAccountDetailsById(userId))
                .orElseGet(AccountDetails::new);
        return RestBean.success(details.asViewObject(AccountDetailsVo.class));
    }

    @PostMapping("save-details")
    public RestBean<Void> saveDetails(@RequestAttribute(Const.ATTR_USER_ID) int userId,
                                      @RequestBody @Valid DetailsSaveVo vo) {
        boolean success = accountDetailsService.saveAccountDetails(userId, vo);
        return success ? RestBean.success() : RestBean.failure(400, "保存失败,用户名已被使用");
    }

    @PostMapping("modify-email")
    public RestBean<Void> modifyEmail(@RequestAttribute(Const.ATTR_USER_ID) int userId,
                                      @RequestBody @Valid ModifyEmailVo vo) {
        String result = accountService.modifyEmail(userId, vo);
        return result == null ? RestBean.success() : RestBean.failure(400, result);
    }

    @PostMapping("change-password")
    public RestBean<Void> changePassword(@RequestAttribute(Const.ATTR_USER_ID) int userId,
                                         @RequestBody @Valid ChangePasswordVo vo) {
        return this.messageHandle(() -> accountService.changePassword(userId, vo));
    }

    @PostMapping("save-privacy")
    public RestBean<Void> savePrivacy(@RequestAttribute(Const.ATTR_USER_ID) int userId,
                                      @RequestBody @Valid PrivacySaveVo vo) {
        accountPrivacyService.savePrivacy(userId, vo);
        return RestBean.success();
    }

    @GetMapping("privacy")
    public RestBean<AccountPrivacyVo> privacy(@RequestAttribute(Const.ATTR_USER_ID) int userId) {
        return RestBean.success(accountPrivacyService.accountPrivacy(userId).asViewObject(AccountPrivacyVo.class));
    }

    private <T> RestBean<T> messageHandle(Supplier<String> action) {
        String msg = action.get();
        return msg == null ? RestBean.success() : RestBean.failure(400, msg);
    }

}


